/*

@description: 单号查询结果JavaScript

@author: 赖赖

@update: 2021年9月12日11:49:25

 */

// 订单号相关数据



// 订单号
const ddhm = JSON.parse(localStorage.getItem('dingdanhao'))
// console.log(ddhm);
// 订单数据
const ddsj = JSON.parse(localStorage.getItem('dingdanshuju'))
// console.log(ddsj);

// 文本框
const wbk = document.querySelector(".wbk")
// console.log(wbk);
wbk.value = ddhm;

// 判断有无数据
if (ddsj.length === 0) {
    const xssj = document.querySelector(".xssj")
    const wxx = document.querySelector(".wxx")
    const zk = document.querySelector(".zongti")
    xssj.style.display = "none";
    wxx.style.display = "block";
    zk.style.paddingBottom = "587px";

} else getjieguo()// 有数据渲染到页面

function getjieguo() {
    const z = document.querySelectorAll(".z")
    // console.log(z);

    // 上门时间转换
    const times = new Date(parseInt(ddsj[0].time_mail));
    const Y = times.getFullYear();
    const M = (times.getMonth() + 1 < 10 ? '0' + (times.getMonth() + 1) : times.getMonth() + 1);
    const D = times.getDate() < 10 ? '0' + times.getDate() : times.getDate();
    const H = times.getHours() < 10 ? '0' + times.getHours() : times.getHours();
    const mi = times.getMinutes() < 10 ? '0' + times.getMinutes() : times.getMinutes();
    const S = times.getSeconds() < 10 ? '0' + times.getSeconds() : times.getSeconds();
    // console.log(Y+M+D,H+mi+S);
    // 下单时间转换
    const xtimes = new Date(parseInt(ddsj[0].ctime_mail));
    const xY = xtimes.getFullYear();
    const xM = (xtimes.getMonth() + 1 < 10 ? '0' + (xtimes.getMonth() + 1) : xtimes.getMonth() + 1);
    const xD = xtimes.getDate() < 10 ? '0' + xtimes.getDate() : xtimes.getDate();
    const xH = xtimes.getHours() < 10 ? '0' + xtimes.getHours() : xtimes.getHours();
    const xmi = xtimes.getMinutes() < 10 ? '0' + xtimes.getMinutes() : xtimes.getMinutes();
    const xS = xtimes.getSeconds() < 10 ? '0' + xtimes.getSeconds() : xtimes.getSeconds();
    // console.log(xY+xM+xD,xH+xmi+xS);

    z[3].innerHTML = ddsj[0].order_mail
    z[12].innerHTML = ddsj[0].name_mail
    z[13].innerHTML = ddsj[0].phone_mail
    z[14].innerHTML = ddsj[0].address_mail
    z[20].innerHTML = ddsj[0].jname_mail
    z[21].innerHTML = ddsj[0].jphone_mail
    z[22].innerHTML = ddsj[0].jaddress_mail
    z[4].innerHTML = Y + '-' + M + '-' + D
    z[5].innerHTML = xY + '-' + xM + '-' + xD + ' ' + xH + ':' + xmi + ':' + xS
}





// 查询按钮
const anjian = document.querySelector(".anjian")
anjian.onclick = function () {
    if (wbk.value === '') {
        alert("请输入订单号！")
    }
    else {
        // console.log(wbk.value);
        getCX(wbk.value)
    }
}
function getCX(order_mail) {
    $.ajax({
        url: `http://127.0.0.1:7001/api/mail/new?order_mail=${order_mail}`,
        method: 'get',
        data: {
            order_mail: wbk.value
        },
        success: function (res) {
            // console.log(res);
            localStorage.setItem('dd', JSON.stringify(res.data))
            //   console.log(localStorage.getItem('dd'));
            const ddnr = JSON.parse(localStorage.getItem('dd'))
            // console.log(ddnr);
            if (ddnr.length === 0 ) {
                const xssj = document.querySelector(".xssj")
                const wxx = document.querySelector(".wxx")
                const zk = document.querySelector(".zongti")
                xssj.style.display = "none";
                wxx.style.display = "block";
                zk.style.paddingBottom = "587px";

            } else getjie()
            function getjie() {
                // console.log(res.data[0]);
                const zk = document.querySelector(".zongti")
                const xssj = document.querySelector(".xssj")
                const z = document.querySelectorAll(".z")
                const wxx = document.querySelector(".wxx")
                wxx.style.display = "none";
                xssj.style.display = "table";
                zk.style.paddingBottom = "150px";
                // console.log(z);

                // 上门时间转换
                const times = new Date(parseInt(res.data[0].time_mail));
                const Y = times.getFullYear();
                const M = (times.getMonth() + 1 < 10 ? '0' + (times.getMonth() + 1) : times.getMonth() + 1);
                const D = times.getDate() < 10 ? '0' + times.getDate() : times.getDate();
                const H = times.getHours() < 10 ? '0' + times.getHours() : times.getHours();
                const mi = times.getMinutes() < 10 ? '0' + times.getMinutes() : times.getMinutes();
                const S = times.getSeconds() < 10 ? '0' + times.getSeconds() : times.getSeconds();
                // console.log(Y+M+D,H+mi+S);
                // 下单时间转换
                const xtimes = new Date(parseInt(res.data[0].ctime_mail));
                const xY = xtimes.getFullYear();
                const xM = (xtimes.getMonth() + 1 < 10 ? '0' + (xtimes.getMonth() + 1) : xtimes.getMonth() + 1);
                const xD = xtimes.getDate() < 10 ? '0' + xtimes.getDate() : xtimes.getDate();
                const xH = xtimes.getHours() < 10 ? '0' + xtimes.getHours() : xtimes.getHours();
                const xmi = xtimes.getMinutes() < 10 ? '0' + xtimes.getMinutes() : xtimes.getMinutes();
                const xS = xtimes.getSeconds() < 10 ? '0' + xtimes.getSeconds() : xtimes.getSeconds();
                // console.log(xY+xM+xD,xH+xmi+xS);

                z[3].innerHTML = res.data[0].order_mail
                z[12].innerHTML = res.data[0].name_mail
                z[13].innerHTML = res.data[0].phone_mail
                z[14].innerHTML = res.data[0].address_mail
                z[20].innerHTML = res.data[0].jname_mail
                z[21].innerHTML = res.data[0].jphone_mail
                z[22].innerHTML = res.data[0].jaddress_mail
                z[4].innerHTML = Y + '-' + M + '-' + D
                z[5].innerHTML = xY + '-' + xM + '-' + xD + ' ' + xH + ':' + xmi + ':' + xS
            }

        },
        error: function () {
            console.log("该订单不存在！");
        }
    })
}
